package com.gitee.wsl.android.ui.sample.icon


import android.animation.ValueAnimator
import android.animation.ArgbEvaluator
import android.graphics.*
import android.view.animation.AccelerateDecelerateInterpolator


/**
 * Created by codeest on 16/11/9.
 *
 * 我把它做成了SwitchView...不知道设计师是不是这个想法..  0.0
 */
class ENScrollView  : AnimatedIcon() {
    private var mCurrentState = STATE_UNSELECT
    private var mCurrentColor = DEFAULT_LINE_COLOR
    private var mPaint: Paint? = null
    private var mBgPaint: Paint? = null
    private var mColorPaint: Paint? = null
    private var mPath: Path? = null
    private var mDstPath: Path? = null
    private var mPathMeasure: PathMeasure? = null
    private var mRectF: RectF? = null
    private var mFraction = 0f
    private var mWidth = 0f
    private var mHeight = 0f
    private var mCenterX = 0f
    private var mCenterY = 0f
    private var mCircleRadius = 0f
    private var mPathLength = 0f
    private var mLineColor = 0
    private var mBgLineColor = 0
    private var mDuration = 0
    private var mViewTranslationX = 0

   /* constructor(context: Context?) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.scroll)
        val lineColor = ta.getColor(R.styleable.scroll_scroll_line_color, DEFAULT_LINE_COLOR)
        val lineWidth = ta.getInteger(R.styleable.scroll_scroll_line_width, DEFAULT_LINE_WIDTH)
        val bgLineColor =
            ta.getInteger(R.styleable.scroll_scroll_bg_line_color, DEFAULT_BG_LINE_COLOR)
        val bgLineWidth =
            ta.getInteger(R.styleable.scroll_scroll_bg_line_width, DEFAULT_BG_LINE_WIDTH)
        val isSelected = ta.getBoolean(R.styleable.scroll_scroll_is_selected, DEFAULT_STATE)
        val viewTranslationX =
            ta.getInteger(R.styleable.scroll_scroll_view_translation_x, DEFAULT_VIEW_TRANSLATION_X)
        ta.recycle()

    }*/

    init {
        //mLineColor = DEFAULT_BG_LINE_COLOR
       // mBgLineColor = bgLineColor
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeCap = Paint.Cap.ROUND
        //mPaint!!.color = lineColor
        //mPaint!!.strokeWidth = lineWidth.toFloat()
        mBgPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mBgPaint!!.style = Paint.Style.STROKE
        mBgPaint!!.strokeCap = Paint.Cap.ROUND
        //mBgPaint!!.color = bgLineColor
        //mBgPaint!!.strokeWidth = bgLineWidth.toFloat()
        mColorPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mColorPaint!!.style = Paint.Style.FILL
        mColorPaint!!.strokeCap = Paint.Cap.ROUND
        mColorPaint!!.color = mCurrentColor
        mPath = Path()
        mDstPath = Path()
        mPathMeasure = PathMeasure()
        mDuration = DEFAULT_DURATION
        mViewTranslationX = DEFAULT_VIEW_TRANSLATION_X
        if (isSelected()) {
            mFraction = 1f
            mCurrentState = STATE_SELECT
        } else {
            mFraction = 0f
            mCurrentState = STATE_UNSELECT
        }
    }

     fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mHeight = (h * 4 / 5).toFloat()
        mWidth = h * 2.5f //控件长宽比定义为 5 : 2
        mCenterX = (w / 2).toFloat()
        mCenterY = (h / 2).toFloat()
        mCircleRadius = mHeight / 2
        mRectF = RectF(
            mCenterX - mCircleRadius * 2, mCenterY - mCircleRadius,
            mCenterX + mCircleRadius * 2, mCenterY + mCircleRadius
        ) //外圆矩形长宽比定义为 2 : 1
        mPath!!.addRoundRect(mRectF!!, mCircleRadius, mCircleRadius, Path.Direction.CW)
        mPathMeasure!!.setPath(mPath, false)
        mPathLength = mPathMeasure!!.length
    }

    override fun draw(canvas: Canvas) {
        canvas.save()
        canvas.translate(mFraction * mViewTranslationX, 0f)
        canvas.drawRoundRect(mRectF!!, mCircleRadius, mCircleRadius, mBgPaint!!) //嗷~ 在这画背景线
        mDstPath!!.reset()
        mPathMeasure!!.getSegment(0f, mPathLength / 2 * mFraction, mDstPath, true)
        canvas.drawPath(mDstPath!!, mPaint!!) //嗷~ 在这画上半圈
        mDstPath!!.reset()
        mPathMeasure!!.getSegment(
            mPathLength - mPathLength / 2 * mFraction,
            mPathLength,
            mDstPath,
            true
        )
        canvas.drawPath(mDstPath!!, mPaint!!) //嗷~ 在这画下半圈
        mColorPaint!!.color = mCurrentColor
        if (mFraction <= 0.8 && mFraction > 0.2) {  //嗷~ 在这画球
            canvas.drawCircle(
                mCenterX - mCircleRadius + 2 * mCircleRadius * mFraction,
                mCenterY,
                mCircleRadius / 6 / 0.6f * (mFraction - 0.2f),
                mColorPaint!!
            )
        } else if (mFraction > 0.8) {
            canvas.drawCircle(
                mCenterX - mCircleRadius + 2 * mCircleRadius * mFraction,
                mCenterY,
                mCircleRadius / 6,
                mColorPaint!!
            )
        }
        canvas.restore()
    }

    fun select() {
        if (mCurrentState == STATE_SELECT) {
            return
        }
        mCurrentState = STATE_SELECT
        val valueAnimator = ValueAnimator.ofObject(
            ArgbEvaluator(),
            mBgLineColor, mLineColor
        )
        valueAnimator.duration = mDuration.toLong()
        valueAnimator.interpolator = AccelerateDecelerateInterpolator()
        valueAnimator.addUpdateListener { valueAnimator ->
            mFraction = valueAnimator.animatedFraction
            mCurrentColor = valueAnimator.animatedValue as Int
            invalidateSelf()
        }
        if (!valueAnimator.isRunning) {
            valueAnimator.start()
        }
        valueAnimator.start()
    }

    fun unSelect() {
        if (mCurrentState == STATE_UNSELECT) {
            return
        }
        mCurrentState = STATE_UNSELECT
        val valueAnimator = ValueAnimator.ofObject(
            ArgbEvaluator(),
            mLineColor, mBgLineColor
        )
        valueAnimator.duration = mDuration.toLong()
        valueAnimator.interpolator = AccelerateDecelerateInterpolator()
        valueAnimator.addUpdateListener { valueAnimator ->
            mFraction = 1 - valueAnimator.animatedFraction
            mCurrentColor = valueAnimator.animatedValue as Int
            invalidateSelf()
        }
        if (!valueAnimator.isRunning) {
            valueAnimator.start()
        }
        valueAnimator.start()
    }

     fun isSelected(): Boolean {
        return mCurrentState == 0
    }

     fun setDuration(duration: Int) {
        mDuration = duration
    }

    override fun startAnimation()  {
        select()
    }

     fun pause() {
        unSelect()
    }

    companion object {
        private const val STATE_SELECT = 0
        private const val STATE_UNSELECT = 1
        private const val DEFAULT_LINE_COLOR = Color.WHITE
        private const val DEFAULT_BG_LINE_COLOR = -0xbbb5b1
        private const val DEFAULT_LINE_WIDTH = 10
        private const val DEFAULT_BG_LINE_WIDTH = 10
        private const val DEFAULT_DURATION = 700
        private const val DEFAULT_VIEW_TRANSLATION_X = 16
        private const val DEFAULT_STATE = false
    }
}